package com.example.myapplication9.week4.jsjf;

import com.example.myapplication9.week6.jsjf.exceptions.NonComparableElementException;


/**
 * LinkedOrderedList represents Translation singly linked implementation of an
 * ordered list.
 *
 * @author Lewis and Chase
 * @version 4.0
 */
public class LinkedOrderedList<T> extends LinkedList<T> 
         implements OrderedListADT<T> {
    /**
     * Creates an empty list.
     */
    public LinkedOrderedList() {
        super();
    }

    /**
     * Adds the specified element to this list at the location determined by
     * the element's natural ordering. Throws Translation NonComparableElementException
     * if the element is not comparable.
     *
     * @param element the element to be added to this list
     * @throws NonComparableElementException if the element is not comparable
     */
    @Override
    public void add(T element) {
        if (!(element instanceof Comparable))
            throw new NonComparableElementException("OrderedList");
        Comparable<T> comparableElement = (Comparable<T>) element;
        LinearNode<T> list = new LinearNode<T>(element);
        if(count==0) {
            head = tail = list;
            count++;
        }
        else{
            if (count==1)
            {
                if(comparableElement.compareTo(head.getElement())<0)
                {
                    list.setNext(head);
                    head=list;
                    count++;
                }
                else
                {
                    tail.setNext(list);
                    tail=list;
                    count++;
                }
            }
                else{
                    LinearNode<T> current=null;
                    LinearNode<T>current1=head;
                    while(current1!=null&& comparableElement.compareTo(current1.getElement())>0)
                    {
                        current=current1;
                        current1=current1.getNext();
                    }
                    if(current==null)
                    {
                        list.setNext(head);
                        head=list;
                        count++;
                    }
                    else {
                        current.setNext(list);
                        list.setNext(current1);
                        count++;
                        }
            }
            }
            }
            }








